// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
Vue.config.productionTip = false


//引入模块
var axios = require("axios")
// //设置baseurl
axios.defaults.baseURL = "http://localhost:8088/hotel/"
// axios.defaults.headers.common['token'] = sessionStorage.getItem("user");
axios.interceptors.request.use(config => {
  config.headers['token'] = sessionStorage.getItem("user");
  return config
}, err => {
})
// //全局注册，之后可以在其他组件中使用this.$axios发生数据
Vue.prototype.$axios = axios




/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>',
  created() {
    router.beforeEach((to, from, next) => {
      if (to.meta.requireAuth) { // 如果访问的页面必须要登录
        if (sessionStorage.getItem("user") == null) { //检查当前如果已经登陆过
          next({
            path: '/', //path到登录页面
            // query: { redirect: to.fullPath }
          })
        } else { //否则先登录
          next(); //如果已经登录，则继续执行
        }
      } else { //不需要登录则不执行其他操作
        next();
      }
    })
  }
})
